<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="创建时间">
        <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:termtime:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:termtime:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:termtime:remove']"
        >删除</el-button>
      </el-col>
    </el-row> -->

    <el-table v-loading="loading" :data="userTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="任务名称" align="center" prop="tetaName" :show-overflow-tooltip="true" >
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.tetiBeginYear}}~{{scope.row.tetiEndYear}}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="受评人" align="center" prop="tetaCoverUsername" />
      <el-table-column label="状态" align="center" prop="teutStatus" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
            <span v-if="scope.row.teutStatus == 0">
                未读（未查看）
            </span>
            <span v-else-if="scope.row.teutStatus == 1">
                已读（未开始）
            </span>
            <span v-else-if="scope.row.teutStatus == 2">
                进行中（未提交）
            </span>
            <span v-else-if="scope.row.teutStatus == 3">
                已完成（强制结束）
            </span>
            <span v-else-if="scope.row.teutStatus == 4">
                已完成（正常结束）
            </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  circle type="success"
                  icon="el-icon-star-on"
                  @click="handleSocre(scope.row)"
                  v-hasPermi="['teacher:evaluation:mytask:list']" title="打分"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学年开始年" prop="tetiBeginYear" required="true">
          <el-input type="input"  v-model="form.tetiBeginYear" placeholder="请输入学年开始年（4位的年份，如2022）"    />
        </el-form-item>
        <el-form-item label="学年结束年" prop="tetiEndYear" required="true" >
          <el-input type="input"  v-model="form.tetiEndYear" placeholder="请输入学年结束年份（4位的年份，如2023）" />
        </el-form-item>
        <el-form-item label="学期" prop="tetiTerm" required="true">
          <el-radio-group v-model="form.tetiTerm">
            <el-radio
              :key="1"
              :label="1"
            > 1
            </el-radio>
            <el-radio
              :key="2"
              :label="2"
            > 2
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学期开始日" prop="tetiTermBeginDate" required="true" >
          <el-date-picker type="date" v-model="form.tetiTermBeginDate" placeholder="请选择学期开始日"  />
        </el-form-item>
        <el-form-item label="学期结束日" prop="tetiTermEndDate" required="true">
          <el-date-picker type="date" v-model="form.tetiTermEndDate" placeholder="请选择学期结束日" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUserTask, getUserTask} from "@/api/teacher/evaluation/userTask.js";

export default {
  name: "termTime",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户任务表格数据
      userTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        type: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tetiBeginYear: [
          { required: true, message: "学年起始年不能为空", trigger: "blur" }
        ],
        tetiEndYear: [
          { required: true, message: "学年结束年不能为空", trigger: "blur" }
        ],
        tetiTerm: [
          { required: true, message: "学年的学期不能为空", trigger: "blur" }
        ],
        tetiTermBeginDate: [
          { required: true, message: "学年学期开始日不能为空", trigger: "blur" }
        ],
        tetiTermEndDate: [
          { required: true, message: "学年学期结束日不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    /*this.getDicts("t_sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });*/
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      listUserTask(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userTaskList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学年学期";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTermtime(id).then(response => {
        this.form = response.data;
        console.log("日期转换前", this.form);
        //this.form.tetiTermBeginDate = this.dateFormateBegin(this.form.tetiTermBeginDate);
        //this.form.tetiTermEndDate = this.dateFormateEnd(this.form.tetiTermEndDate);
        this.open = true;
        this.title = "修改学年学期";
      });
    },
    handleSocre(row) {
      this.$router.push({path:"/teacher/evaluation/mytask/taskScoreData", query:{teutId: row.teutId}})
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateTermtime(this.form.id, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            addTermtime(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除学年学期编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTermtime(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportType(queryParams);
      }).then(response => {
        this.fileDownload(response.data, "学年学期.xlsx");
      }).catch(function() {});
    },
    /** 格式化日期 */
    dateFormateBegin:function (date) {
      if (date == undefined) {
        return ""
      } else {
        return moment(date).format("YYYY-MM-DD 00:00:00");
      }
    },
     /** 格式化日期 */
     dateFormateEnd: function(date) {
      if (date == undefined) {
        return ""
      } else {
        return moment(date).format("YYYY-MM-DD 23:59:59");
      }
    }

  }
};
</script>
